<?php
/**
 * Convenience functions for Unfuddle
 *
 * @todo Remove anything that isn't being used
 */
class Unfuddle {

    /**
     * Returns all digits within a string
     */
    static public function get_digits($input) {
    
        return preg_replace('/[^0-9]/', '', $input);
    
    }
    
    /**
     * Returns all alphabetical characters within a string
     */
    static public function get_alpha($input) {
    
        return preg_replace('/[^a-z]/i', '', $input);
    
    }
    
    /**
     * Returns all alpha-numerical characters within a string
     */
    static public function get_alnum($input) {
    
        return preg_replace('/[^a-z0-9]/i', '', $input);
    
    }
    
    /**
     * Get a column from an array. Basically, this searches an
     * array for $column as a key and returns any values in the array
     * that has that key.
     * This only really works with a two-dimansional array. For instance:
     *
     * array(
     *     array('id' => 1, 'name' => 'Bobby', 'is_active' => 1),
     *     array('id' => 2, 'name' => 'Ralphie', 'is_active' => 1),
     *     array('id' => 3, 'name' => 'Johnny', 'is_active' => 0)
     * )
     * 
     * In that kind of array, you could specify "name" as the column and get
     * back Bobby, Ralphie and Johnny in an array.
     * @todo Test this out thoroughly. 
     */
    static public function array_column($array, $column) {
    
        $return = array();
        foreach ($array as $key => $value) {
        if (is_array($value) && array_key_exists($column, $value)) {
            $return[] = $value[$column];
        }
        }
        return $return;
    
    }
    
    /**
     * Perform a deep string replace operation to ensure the values in $search are no longer present
     *
     * Repeats the replacement operation until it no longer replaces anything so as to remove "nested" values
     * e.g. $subject = '%0%0%0DDD', $search ='%0D', $result ='' rather than the '%0%0DD' that
     * str_replace would return
     *
     * @param string|array $search
     * @param string $subject
     * @return string The processed string
     */
    static public function deep_str_replace($search, $subject) {
    
        $found = true;
        $subject = (string) $subject;
        while ($found) {
            $found = false;
            foreach ((array) $search as $val) {
                while (strpos($subject, $val) !== false) {
                    $found = true;
                    $subject = str_replace($val, '', $subject);
                }
            }
        }
        return $subject;
    
    }
    
    /**
     * Returns one of an array's elements specified by a key. No errors are
     * triggered. If the array element does not exist $default is returned instead.
     *
     * $key may specified as an array for multi-dimensional access.
     *
     * @param array $array
     * @param mixed $key
     * @param mixed $default
     * @return mixed
     */
    static public function dindex($array, $key, $default = null) {
    
        foreach ((array)$key as $dimension_key) {
            if (!isset($array[$dimension_key])) {
                return $default;
            }
            $array = $array[$dimension_key];
        }
        return $array;
    
    }
    
    /**
     * Returns one of an array's elements specified by a key. Errors are triggered
     * if the array element does not exist.
     *
     * $key may specified as an array for multi-dimensional access.
     *
     * @param array $array
     * @param mixed $key
     * @return mixed
     */
    static public function index($array, $key) {
    
        foreach ((array)$key as $dimension_key) {
            if (!isset($array[$dimension_key])) {
                flu_undefined_index_error($dimension_key);
                return null;
            }
            $array = $array[$dimension_key];
        }
        return $array;
    
    }
    
    /**
     * Return multiple array elements specified by an array of keys. Errors are
     * triggered if array elements do not exist.
     *
     * @param array $array
     * @param array $keys
     * @param bool $preserve_keys
     * @return array
     */
    static public function mindex($array, $keys, $preserve_keys = false) {
    
        $out = array();
        foreach ($keys as $key) {
            if ($preserve_keys) {
                $out[$key] = index($array, $key);
            } else {
                $out[] = index($array, $key);
            }
        }
        return $out;
    
    }
    
    /**
     * Return multiple array elements specified by an array of keys. No errors are
     * triggered. If array elements do not exist the value of $default is used instead.
     *
     * @param array $array
     * @param array $keys
     * @param bool $preserve_keys
     * @return array
     */
    static public function dmindex($array, $keys, $preserve_keys = false, $default = null) {
    
        $out = array();
        foreach ($keys as $key) {
            if ($preserve_keys) {
                $out[$key] = dindex($array, $key, $default);
            } else {
                $out[] = dindex($array, $key, $default);
            }
        }
        return $out;
    
    }
    
    /**
     * Returns $obj
     *
     * @see README for why this is any use
     * @param mixed $obj
     * @return mixed
     */
    static public function with($obj) {
    
        return (object)$obj;
    
    }
    
    /**
     * Like func_get_args() without restrictions
     *
     * @return array
     */
    static public function args() {
    
        return dindex(dindex(debug_backtrace(), 1), 'args');
    
    }
    
    /**
     * var_dump() only $var is returned
     *
     * @param mixed $var
     * @return mixed
     */
    static public function dump($var) {
    
        var_dump($var);
        return $var;
    
    }
    
    /**
     * Returns the output from var_dump($var)
     *
     * @param mixed $var
     * @return string
     */
    static public function sdump($var) {
    
        ob_start();
        var_dump($var);
        $out = ob_get_contents();
        ob_end_clean();
        return $out;
    
    }
    
    /**
     * Create an array from whitespace separated elements. Can save a lot of typing
     * in some instances.
     *
     * @param string $elements
     * @return array
     */
    static public function w($elements) {
    
        return preg_split('~\s+~', $elements, -1, PREG_SPLIT_NO_EMPTY);
    
    }

}